<template>
  <a-spin :loading="loading" :tip="isLogin() ? '绑定中...' : '登录中...'">
    <div></div>
  </a-spin>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import http from '@/utils/http'
import { setToken } from '@/utils/auth'

const loading = ref(true)
const router = useRouter()
// handleWeComCallback 页面回调
const handleWeComCallback = async (code, state) => {
  // 向服务器端发送code和state进行验证
  try {
    const response = await http.post('/api/plugin/wework/auth/callback', {
      code,
      state,
    })
    if (response.success) {
      // 登录成功，保存token信息
      setToken(response.data.token)
      router.push('/')
    }
  } catch (error) {
    console.error('企业微信登录回调处理失败:', error)
    router.push('/login')
  }
}
const checkCallback = () => {
  const urlParams = new URLSearchParams(window.location.search)
  const code = urlParams.get('code')
  const state = urlParams.get('state')

  if (code && state) {
    handleWeComCallback(code, state)
  }
}

// 组件挂载时检查是否是回调
onMounted(() => {
  checkCallback()
})
</script>
